package com.pls.action;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.pls.actionObjects.SearchRequest;
import com.pls.actionObjects.SearchResponse;
import com.pls.constants.ValidationErrorEnum;
import com.pls.dao.DAOFactory;
import com.pls.dao.EmployeeDAO;
import com.pls.domain.Employee;
import com.pls.domain.ExceptionError;
import com.pls.domain.ValidationError;

public class SearchEmployeeHandler extends
		BaseRequestHandler<SearchResponse, SearchRequest> {

	Logger logger = Logger.getLogger(SearchEmployeeHandler.class);

	public SearchEmployeeHandler(Class<SearchResponse> tResponse) {
		super(tResponse);
		// TODO Auto-generated constructor stub
	}

	protected ArrayList<ValidationError> validate(SearchRequest request) {
		logger.debug(this.getClass() + "- method validate- entered");
		ArrayList<ValidationError> errors = new ArrayList<ValidationError>();
		ValidationError valError;

		// Check if request is null
		if (request == null) {
			valError = new ValidationError();
			valError.setMessage("Request is null.");
			valError.setErrorName(ValidationErrorEnum.NullRequest);
			errors.add(valError);
			logger.debug(this.getClass() + "- null request. Exiting validate method.");
			return errors;
		}
		// Check if request empId is 0
//		if (request.getEmployeeId() == 0 && request.getEmployeeName() == null
//				&& request.getProjectName() == null) {
//			valError = new ValidationError();
//			valError.setMessage("Request parameters cannot be null.");
//			valError.setErrorName(ValidationErrorEnum.InvalidRequestParameter);
//			logger.debug(this.getClass() + "- null request PARAMETERS.");
//			errors.add(valError);
//		}
		 //check if search query is null
		if (request.getSearchQuery() == null) {
			valError = new ValidationError();
			valError.setMessage("Search query of request cannot be null.");
			valError.setErrorName(ValidationErrorEnum.InvalidRequestParameter);
			errors.add(valError);
		}

		logger.debug(this.getClass() + "- exiting validate.");
		return errors;
	}

	protected SearchResponse process(SearchRequest request) {

		SearchResponse response = new SearchResponse();
		// call data layer
		EmployeeDAO empDao = DAOFactory.getEmployeeDAO();
		List<Employee> wildCardList = new ArrayList<Employee>();

		try {
			logger.debug("Process for SearchResponseHandler entered.");
			
			//Try to parse the search query request if employee id is entered
			if(isInteger(request.getSearchQuery())){
				
				int empId = Integer.parseInt(request.getSearchQuery());
				wildCardList.addAll(empDao.getEmployeeWildCardById(empId));
			}else{
				// Get the wild card employee names
				wildCardList.addAll(empDao.getEmployeeWildCardByName(request.getSearchQuery()));		
			}

			//TODO Get wildcard for projects	
			
			//set the response
			response.setSearchResults(wildCardList);

		} catch (SQLException e) {
			// ADD LOGGER
			ExceptionError sqlError = new ExceptionError(e.getClass()
					.toString(),
					"Sorry, an SQL error occurred while accessing the DB.");
			response.setExceptionError(sqlError);
			logger.debug(this.getClass() + "- sql exception:" + e.getMessage());
		} catch (Exception e) {
			ExceptionError sqlError = new ExceptionError(e.getClass()
					.toString(),
					"Sorry, an error occurred while accessing the DB.");
			response.setExceptionError(sqlError);
			logger.debug(this.getClass() + "- exception:" + e.getMessage());
		}
		
		logger.debug(this.getClass() + "- process method exited.");
		return response;
	}
	
	
	//Private methods
	
	//TODO refactor code
	private static boolean isInteger(String s) {
	    try { 
	        Integer.parseInt(s); 
	    } catch(NumberFormatException e) { 
	        return false; 
	    }
	    // only got here if we didn't return false
	    return true;
	}

}
